package com.jian.mygame;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

class MySound extends Object{
	private static SoundPool sounds;
	//private static int[] sound_target;
	//private static int[] sound_bullet;
	private static HashMap<Integer, Integer> soundPoolMap;
	
	private static int found;
	private static int next;
	private static MediaPlayer music;
	private static boolean mSoundEnable = true;
	
	public static void loadSound(Context context) {
		int i;
		int length;
		int soundID;

		mSoundEnable = true;
	    sounds = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
	    // three ref. to the sounds I need in the application
	    
	    length = MyFactory.mTargetRes.length;
	    soundPoolMap = new HashMap<Integer, Integer>();
	    //sound_target = new int[length];
	    for( i = 0; i < length; i++) {
	    	soundID = MyFactory.mTargetRes[i].mSoundID;
	    	if(soundID > 0){
	    		int soundTrackNo = sounds.load(context, soundID, 1);
	    		soundPoolMap.put(soundID, soundTrackNo);
	    	}
	    }

	    length = MyFactory.mBulletRes.length;
	    //sound_bullet = new int[length];
	    for( i = 0; i < length; i++) {
	    	soundID = MyFactory.mBulletRes[i].mSoundID;
	    	if(soundID > 0){
	    		//sound_target[i] = sounds.load(context, soundID, 1);
	    		soundPoolMap.put(soundID, sounds.load(context, soundID, 1));
	    	}
	    }
	    
	    // the music that is played at the beginning and when there is only 10 seconds left in a game
	    music = MediaPlayer.create(context, R.raw.bird3);
	}
	
	public static void playSound( int soundID ) {
	    if (!mSoundEnable) return; // if sound is turned off no need to continue
	    //sounds.play(sound_target[track_no], 1, 1, 1, 0, 1);
	    if( soundID > 0 ) {
		    int soundTrackNo = soundPoolMap.get(soundID);
		    if ( soundTrackNo >= 0 ) {
		    	sounds.play(soundTrackNo, 1, 1, 1, 0, 1);
		    }
	    }
	    //sounds.
	}
	
	public static final void playMusic() {
	    if (!mSoundEnable) return;
	    if (!music.isPlaying()) {
	    music.seekTo(0);
	    music.start();
	    }
	}
	
	public static final void pauseMusic() {
	    if (!mSoundEnable) return;
	    if (music.isPlaying()) music.pause();
	}
	
	/*
	 * In the onDestroy method of the activity 
	 * call the following method to release all the handles on sound or music objects.
	 */
	public static final void release() {
	    sounds.release();
	    music.stop();
	    music.release();
	}
}
